package com.heisenhub.logs.common.qo;

import com.heisenhub.common.oauth.entity.UuaUser;
import com.heisenhub.logs.common.dto.LogsRecordsDto;
import com.heisenhub.logs.common.enums.LogsOperateEnums;
import com.heisnenhub.common.module.constant.HttpConstant;
import com.heisnenhub.common.module.enmus.BusinessTypeEnums;
import com.heisnenhub.common.module.exception.RRException;
import com.heisnenhub.common.module.inter.BusinessTypeEnumsInter;
import com.heisnenhub.common.module.utils.GsonUtils;
import com.heisnenhub.common.module.utils.HubObjectUtils;
import com.heisnenhub.common.module.utils.ServiceNameUtils;
import com.heisnenhub.common.module.utils.TimeUtils;
import lombok.Data;

import java.util.ArrayList;
import java.util.Optional;

/**
 *  通用QO  （如  日志, 备注）
 */
@Data
public class GMQo {

    private UuaUser user;  // 用户

    private String title; // 标题   //备注时可以不写

    private LogsOperateEnums logsOperateEnums; // 日志操作

    private BusinessTypeEnumsInter enums; // 业务类型

    private String businessUniqueCode;  // 唯一标识 (路订单号)

    /**
     * detail千万 不要写成 对象的json 字符窜
     * <p>
     * 写成java字符窜  或写成List的json字符窜
     */
    private String details; // 详情


    /**
     * 操作 是否成功 true成功  false失败
     * 默认是成功
     */
    private Boolean success = true;

    public GMQo() {
    }


    public GMQo(UuaUser user, String title, BusinessTypeEnumsInter enums, String businessUniqueCode, String details, boolean success ,LogsOperateEnums logsOperateEnums) {
        this.user = user;
        this.title = title;
        this.enums = enums;
        this.businessUniqueCode = businessUniqueCode;
        this.details = details;
        this.success = success;
        this.logsOperateEnums=logsOperateEnums;
    }

    public GMQo(UuaUser user, String title, BusinessTypeEnumsInter enums, String businessUniqueCode, String details ,LogsOperateEnums logsOperateEnums) {
        this.user = user;
        this.title = title;
        this.enums = enums;
        this.businessUniqueCode = businessUniqueCode;
        this.details = details;
        this.logsOperateEnums=logsOperateEnums;
    }


    /**
     * 构建日志
     *
     * @return
     */
    public  LogsRecordsDto buildOperateLog() {
        if (this.getEnums() == null) throw new RRException("分组不能为空");
        Optional<GMQo> qo = Optional.ofNullable(this);
        LogsRecordsDto logOperate = new LogsRecordsDto();
        if (HubObjectUtils.isNotEmpty(this.getUser())) {
            UuaUser currentUser = this.getUser();
            // logOperate.setOperateDept(currentUser.getDeptName());
            logOperate.setJobName(currentUser.getUsername());
            logOperate.setJobNo(currentUser.getUsername());
        } else {
            //  logOperate.setOperateDept("--");
            logOperate.setJobName("--");
            logOperate.setJobNo("--");
        }


        if (this.getSuccess()) logOperate.setStatus(HttpConstant.HttpCode.SC_OK);
        else logOperate.setStatus(HttpConstant.HttpCode.SC_FAILE);


        logOperate.setCreateTime(TimeUtils.nowDate());
        logOperate.setOperateGroup(qo.map(GMQo::getEnums).orElse(BusinessTypeEnums.NONE).getBusinessType());
        logOperate.setServiceName(ServiceNameUtils.serviceName);

        LogsOperateEnums logsOperateEnums = qo.map(GMQo::getLogsOperateEnums).get();
        logOperate.setOperateType(logsOperateEnums.ordinal());
        logOperate.setOperateTitle(qo.map(GMQo::getTitle).orElseThrow(() -> new RRException("日志 标题为空")));
        logOperate.setBusinessUniqueCode(qo.map(GMQo::getBusinessUniqueCode).orElseThrow(() -> new RRException("日志 唯一标识为空")));
        if (HubObjectUtils.isEmpty(qo.get().getDetails())) throw new RRException("日志详情为空");

        boolean json = GsonUtils.isJsonList(qo.get().getDetails());

        if (!json) {
            ArrayList<String> litsStr = new ArrayList<>();
            litsStr.add(qo.map(GMQo::getDetails).get());
            logOperate.setDetails(GsonUtils.toJson(litsStr));
        } else logOperate.setDetails(qo.map(GMQo::getDetails).get());

        return logOperate;

    }
}